import 'dart:math' as Math;

import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/model/repay_entity.dart';
import 'package:flutter2/ui/repay.dart';
import 'package:flutter2/utils/num.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/widget/base/base_container.dart';
import 'package:flutter2/widget/base/normal_text.dart';
import 'package:flutter2/widget/info_view.dart';

class ItemRepay extends StatelessWidget {
  const ItemRepay({
    super.key,
    required this.repayPlan,
    required this.check,
    this.click,
    this.totalPeriod = 1,
    this.expand,
    this.expandClick,
  });

  final RepayPlanEntity repayPlan;
  final bool check;
  final VoidCallback? click;
  final int totalPeriod;
  final bool? expand;
  final VoidCallback? expandClick;

  @override
  Widget build(BuildContext context) {
    return BaseContainer(
      width: double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 16, vertical: 5),
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
      radius: 10,
      background: Colors.white,
      child: Column(
        children: [
          Row(
            children: [
              if (unComplete(repayPlan) && totalPeriod > 1)
                GestureDetector(
                  onTap: click,
                  child: BaseContainer(
                    backgroundImg: check ? "cb_checked".toImage() : "cb_unchecked".toImage(),
                    width: 16,
                    height: 16,
                  ),
                ),
              if (unComplete(repayPlan) && totalPeriod > 1) SizedBox(width: 5),
              if (totalPeriod > 1)
                NormalText(
                  text: "Cuotas: ${repayPlan.cur}",
                  fontSize: 15,
                  fontColor: mainText,
                )
            ],
          ),
          SizedBox(
            height: 5,
          ),
          InfoView(
            leftText: "Pago",
            rightText: repayPlan.repay.formatAmount(),
          ),
          InfoView(
            leftText: "Vencimiento",
            rightText: repayPlan.repayTime,
          ),
          if (expand == true) ...[
            InfoView(
              hide: repayPlan.overDays <= 0,
              leftText: "Días de Vencido",
              rightText: repayPlan.overDays.toString(),
            ),
            InfoView(
              hide: repayPlan.loan <= 0,
              leftText: "Capital",
              rightText: repayPlan.loan.formatAmount(),
            ),
            InfoView(
              hide: repayPlan.interest <= 0,
              leftText: "Intereses",
              rightText: repayPlan.interest.formatAmount(),
            ),
            InfoView(
              hide: repayPlan.service <= 0,
              leftText: "Cuota servicio",
              rightText: repayPlan.service.formatAmount(),
            ),
            InfoView(
              hide: repayPlan.late <= 0,
              leftText: "Mora",
              rightText: repayPlan.late.formatAmount(),
            ),
          ],
          Transform.rotate(
            angle: expand == true ? Math.pi : 0,
            child: BaseContainer(
              backgroundImg: "ic_detail_down".toImage(),
              width: 16,
              height: 16,
              click: expandClick,
            ),
          )
        ],
      ),
    );
  }
}
